package uit.comstore.model.blo;

import java.util.List;

import org.hibernate.Session;
import uit.comstore.model.entity.Order;
import uit.comstore.utils.HibernateSessionFactory;

public class OrderBLO {
	@SuppressWarnings("unchecked")
	public List<Order> listAllOrdersNotDelivery(){
		List<Order> orders = null;
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		orders = session.createSQLQuery("SELECT * FROM Orders WHERE OrdersId NOT IN(SELECT DISTINCT OrdersId FROM Deliveries)").addEntity(Order.class).list();
		session.getTransaction().commit();
		return orders;
	}
	
	@SuppressWarnings("unchecked")
	public List<Order> listAllOrdersDeliveried(){
		List<Order> orders = null;
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		orders = session.createSQLQuery("SELECT * FROM Orders WHERE OrdersId IN(SELECT DISTINCT OrdersId FROM Deliveries)").addEntity(Order.class).list();
		session.getTransaction().commit();
		return orders;
	}
	
	public Order getById(Long id){
		Order order = null;
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();	
		session.beginTransaction();
		order = (Order) session.get(Order.class, id);
		session.getTransaction().commit();
		return order;
	}
}
